package com.admission.web.admin;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.xwork.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;
import org.springside.modules.utils.web.struts2.Struts2Utils;

import com.admission.model.Employee;
import com.admission.model.Role;
import com.admission.service.EmployeeManager;
import com.admission.service.RoleManager;
import com.admission.util.HibernateUtils;
import com.admission.util.PageUtils;
import com.admission.web.CrudActionSupport;

@Namespace("/admin")
@Results({ @Result(name = CrudActionSupport.RELOAD, location = "employee-admin.action", type = "redirect") })
public class EmployeeAdminAction extends CrudActionSupport<Employee> {

	private static final long serialVersionUID = -353364724628349162L;
	private String id;
	private String ids;
	private Page<Employee> page = new Page<Employee>(
			PageUtils.DEFAULT_PAGE_SIZE);
	private Employee entity;
	private EmployeeManager employeeManager;
	private List<String> checkedRoleIds; // 页面中钩选的角色id列表
	private RoleManager roleManager;

	public Employee getModel() {
		return entity;
	}

	@Override
	public String list() throws Exception {
		List<PropertyFilter> filters = PropertyFilter
				.buildFromHttpRequest(Struts2Utils.getRequest());
		// 设置默认排序方式
		if (!page.isOrderBySetted()) {
			page.setOrderBy("id");
			page.setOrder(Page.ASC);
		}
		page = employeeManager.searchUser(page, filters);
		return SUCCESS;
	}

	@Override
	public String input() throws Exception {
		checkedRoleIds = entity.getRoleIds();
		return INPUT;
	}

	@Override
	public String save() throws Exception {
		try {
			HibernateUtils.mergeByCheckedIds(entity.getRoles(), checkedRoleIds,
					Role.class);
			employeeManager.save(entity);
		} catch (Exception e) {
			this.addActionMessage("操作失败，请重新操作");
		}
		return list();
	}

	@Override
	public String delete() throws Exception {
		try {
			employeeManager.delete(ids);
			this.addActionMessage("操作成功");
		} catch (Exception e) {
			this.addActionMessage("操作失败，请重新操作");
		}
		return RELOAD;
	}

	@Override
	protected void prepareModel() throws Exception {
		if (StringUtils.isEmpty(id)) {
			entity = new Employee();
		} else {
			entity = employeeManager.getEmployeeById(id);
		}

	}

	public List<Role> getAllRoleList() {
		return roleManager.getAllRole();
	}

	public String pwd() {
		return "pwd";
	}

	public String changePwd() {
		HttpServletRequest request = ServletActionContext.getRequest();
		String oldpassword = request.getParameter("oldpassword");
		String newpassword = request.getParameter("newpassword");
		Struts2Utils.renderText(employeeManager.changePassword(oldpassword,
				newpassword));
		return null;
	}

	public String checkLoginName() {
		HttpServletRequest request = ServletActionContext.getRequest();
		String newLoginName = request.getParameter("loginName");
		String oldLoginName = request.getParameter("oldLoginName");

		if (employeeManager.isLoginNameUnique(newLoginName, oldLoginName)) {
			Struts2Utils.renderText("true");
		} else {
			Struts2Utils.renderText("false");
		}
		return null;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public Page<Employee> getPage() {
		return page;
	}

	public void setPage(Page<Employee> page) {
		this.page = page;
	}

	public Employee getEntity() {
		return entity;
	}

	public void setEntity(Employee entity) {
		this.entity = entity;
	}

	@Autowired
	public void setEmployeeManager(EmployeeManager employeeManager) {
		this.employeeManager = employeeManager;
	}

	public List<String> getCheckedRoleIds() {
		return checkedRoleIds;
	}

	public void setCheckedRoleIds(List<String> checkedRoleIds) {
		this.checkedRoleIds = checkedRoleIds;
	}

	@Autowired
	public void setRoleManager(RoleManager roleManager) {
		this.roleManager = roleManager;
	}

}
